package cn.hamster3.hamsterworld.command;

import cn.hamster3.hamsterworld.core.DataManager;
import cn.hamster3.hamsterworld.core.Message;
import cn.hamster3.hamsterworld.data.WorldData;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.List;

public class SetSizeCommand extends CommandBase {
    public SetSizeCommand(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public boolean isPlayerCommand() {
        return false;
    }

    @Override
    public String getPermission() {
        return "hw.admin";
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length < 2) {
            sender.sendMessage(Message.notInputWorldName.toString());
            return true;
        }

        WorldData worldData = dataManager.getWorldData(args[1]);
        World world = Bukkit.getWorld(args[1]);
        if (worldData == null && world == null) {
            sender.sendMessage(Message.worldNotExist.toString());
            return true;
        }
        if (args.length < 3) {
            sender.sendMessage("§c请输入边界大小!");
            return true;
        }
        int size;
        try {
            size = Integer.parseInt(args[2]);
        } catch (NumberFormatException ignored) {
            sender.sendMessage("§c请输入一个正整数");
            return true;
        }
        if (size < 0) {
            sender.sendMessage("§c请输入一个正整数");
            return true;
        }
        if (worldData != null) {
            worldData.setBorderSize(size);
        }
        world.getWorldBorder().setSize(size);
        sender.sendMessage("§a世界 " + args[1] + " 的边界已设置为: " + args[2]);
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        ArrayList<String> tab = new ArrayList<>();
        if (!sender.hasPermission("hw.admin")) {
            return tab;
        }
        if (args.length == 2) {
            for (WorldData data : dataManager.getWorldData()) {
                if (!data.getName().toLowerCase().startsWith(args[1].toLowerCase())) {
                    continue;
                }
                tab.add(data.getName());
            }
        }
        return tab;
    }
}
